# Figure2_Long-term 12 month SPI values.R
# Coded by Prof. Alastair J. Potts
# 31 Jan 2025
##########################################
# Use groundhog package for reproducibility
use.groundhog <- TRUE

if (use.groundhog) {
  library(groundhog)  # Load groundhog package
  set.groundhog.folder("C:\\Groundhog_R\\R4.4.1_2024-05-14")  # Set groundhog folder location
  GroundhogDay <- '2024-05-14'  # Define GroundhogDay version
  groundhog.library("tidyverse",GroundhogDay)  # Load required packages using groundhog
  groundhog.library("ggrepel",GroundhogDay) 
} else {
  library("tidyverse")  # Load required libraries normally if not using groundhog
  library("ggrepel")
}


# Set working directory
setwd("D:\\Dropbox\\100_PROJECTS\\2024_ThicketHydraulics_SkeltonButtnerPotts")

# Read in the data
read_csv("1_data/07_Choat_etal2012.csv")->
  dat_published
read_csv("1_data/04b_OpticalVulnerabilityCurvesSummaryData.csv")->
  dat_ourdata
read_csv("1_data/01_SpeciesDistribution.csv",skip=4)->
  dat_species_info

dat_ourdata%>%
  select(-species,-1)%>%
  rename(species=tip.label)%>%
  mutate(species=gsub("_"," ",species))->
  dat_ourdata

dat_species_info%>%
  group_by(species)%>%
  summarise(uMAP=mean(MAP))%>%
  left_join(dat_ourdata)->
  forGG_1
  
ggplot()+
  geom_point(data=dat_published,aes(map,p50),colour="grey")+
  geom_text_repel(data=forGG_1,aes(uMAP,P50,label=species),fontface = "italic")+
  geom_point(data=forGG_1,aes(uMAP,P50),fill="steelblue",colour="lightgrey",shape=21,size=2)+
  theme_bw()+
  ylab(expression(paste(P[50]," (MPa)")))+
  xlab("MAP (mm)")+
  scale_y_continuous(breaks=seq(0,-16,-2))
ggsave("3_results/Fig4.jpg",units="cm",height=10,width=10,dpi=900)
